package com.shell.governor.security.user.controller;

import com.shell.bee.base.annotation.Logging;
import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.entity.mvc.RetMsg;
import com.shell.bee.mvc.controller.BaseController;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.framework.utils.VerifyCodeUtil;
import com.shell.governor.security.user.po.PubSecurityPayment;
import com.shell.governor.security.user.service.PubSecurityPaymentService;
import com.shell.governor.security.user.service.SmsCheckService;
import com.shell.governor.user.po.PubUser;
import com.shell.governor.user.service.PubUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class PubPasswordController extends BaseController {
	@Autowired
	PubUserService pubUserService;
	@Autowired
	PubSecurityPaymentService pubSecurityPaymentService;
	@Autowired
	private SmsCheckService smsCheckService;

	@RequestMapping({ "auth/modify-password" })
	@Logging(module = "��������", methods = "������������")
	public RetMsg modifyUserPassword(String loginName, String phoneNo, String newPassword, String srecurityCode) {
		return this.pubUserService.modifyPasswordByLoginNameOrPhoneNo(loginName, phoneNo, newPassword, srecurityCode);
	}

	@RequestMapping({ "auth/check-paypassword-exist" })
	@Logging(module = "��������", methods = "������������������������������")
	public int checkPayPwdExist() {
		PubSecurityPayment security = new PubSecurityPayment();
		security.setUserId(CommonDataUtil.getUserId());
		security = (PubSecurityPayment) this.pubSecurityPaymentService.selectOne(security);
		if (security != null) {
			return 1;
		}
		return 0;
	}

	@RequestMapping({ "auth/modify-user-paypassword" })
	@Logging(module = "��������", methods = "������������")
	public RetMsg modifyUserPayPassword(String vertificationCode, String newPassword, String oldPassword) {
		RetMsg msg = VerifyCodeUtil.checkVerifyCode(vertificationCode);
		if (!msg.getType().equals("success")) {
			return msg;
		}
		int value = this.pubSecurityPaymentService.modifyUserPayPassword(newPassword, oldPassword);
		if (value > 0) {
			return new RetMsg("success", "������������������");
		}
		return new RetMsg("error", "��������������������������������������");
	}

	@RequestMapping({ "auth/save-user-paypassword" })
	@Logging(module = "��������", methods = "������������")
	public RetMsg saveUserPayPassword(String vertificationCode, String newPassword) {
		RetMsg msg = VerifyCodeUtil.checkVerifyCode(vertificationCode);
		if (!msg.getType().equals("success")) {
			return msg;
		}
		int value = this.pubSecurityPaymentService.saveUserPayPassword(newPassword);
		if (value > 0) {
			return new RetMsg("success", "������������������");
		}
		return new RetMsg("error", "������������������");
	}

	@RequestMapping({ "auth/find-user-paypassword" })
	@Logging(module = "��������", methods = "������������")
	public RetMsg updateFindUserPayPassword(String vertificationCode, String newPassword) {
		RetMsg msg = VerifyCodeUtil.checkVerifyCode(vertificationCode);
		if (!msg.getType().equals("success")) {
			return msg;
		}
		int value = this.pubSecurityPaymentService.updateFindUserPayPassword(newPassword);
		if (value > 0) {
			return new RetMsg("success", "������������������");
		}
		return new RetMsg("error", "������������������");
	}

	@RequestMapping({ "pay-pwd/check-mobile-paypassword-exist" })
	@Logging(module = "������������", methods = "������������������������������")
	public int checkMobilePayPwdExist() {
		PubSecurityPayment security = new PubSecurityPayment();
		security.setUserId(CommonDataUtil.getUserId());
		security = (PubSecurityPayment) this.pubSecurityPaymentService.selectOne(security);
		if (security != null) {
			return 1;
		}
		return 0;
	}

	@RequestMapping({ "pay-pwd/check-old-paypwd" })
	@Logging(module = "������������", methods = "������������������������������")
	public boolean checkOldPayPwd(String payPwd) {
		PubSecurityPayment security = new PubSecurityPayment();
		security.setUserId(CommonDataUtil.getUserId());
		security.setPaymentPwd(payPwd);
		security = (PubSecurityPayment) this.pubSecurityPaymentService.selectOne(security);
		if (security == null) {
			return false;
		}
		return true;
	}

	@RequestMapping({ "pay-pwd/su-mobile-paypwd" })
	@Logging(module = "������������", methods = "������������������")
	public RetMsg saveOrUpdateMobileUserPayPassword(String confirmPayPwd, String newPayPwd, String oldPayPwd) {
		if (!confirmPayPwd.equals(newPayPwd)) {
			return new RetMsg("error", true, "������������������������");
		}
		if (StringUtil.emptyAndNull(oldPayPwd)) {
			int value = this.pubSecurityPaymentService.saveUserPayPassword(newPayPwd);
			if (value > 0) {
				return new RetMsg("success", "������������������");
			}
			return new RetMsg("error", "������������������");
		}
		int value = this.pubSecurityPaymentService.modifyUserPayPassword(newPayPwd, oldPayPwd);
		if (value > 0) {
			return new RetMsg("success", "������������������");
		}
		return new RetMsg("error", "��������������������������������������");
	}

	@RequestMapping({ "pay-pwd/find-pay-password" })
	@Logging(module = "������������", methods = "������������������")
	public RetMsg findPayPassword(String phoneNum, String smsCode, String idCard, String confirmPayPwd,
			String newPayPwd) {
		if (!confirmPayPwd.equals(newPayPwd)) {
			return new RetMsg("error", true, "������������������������");
		}
		String smscode = this.smsCheckService.getSmsCode(phoneNum);
		if ((!smsCode.equals(smsCode)) || (smscode == null)) {
			return new RetMsg("error", true, "������������");
		}
		PubUser u = new PubUser();
		u.setPhoneNo(phoneNum);
		u.setCertNo(idCard);
		PubUser user = (PubUser) this.pubUserService.selectOne(u);
		int value = this.pubSecurityPaymentService.resetPayPassword(user.getUserId(), newPayPwd);
		if (value > 0) {
			return new RetMsg("success", "������������������");
		}
		if (value == -1) {
			return new RetMsg("error", "��������������������");
		}
		return new RetMsg("error", "������������������");
	}
}
